using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;

namespace DomainComponents.XCRM
{
    public class SaleItemValidationRules
    {
        public const string ProductIsRequired = "ProductIsRequired";
        public const string QuantityIsGreaterThanOrEqualZero = "QuantityIsGreaterThanOrEqualZero";
        public const string DiscountIsGreaterThanOrEqualZero = "DiscountIsGreaterThanOrEqualZero";
    }

    [DomainComponent, NavigationItem(false)]
    [XafDisplayName("Sale Item")]
    public interface ISaleItem
    {
        [VisibleInListView(false)]
        ISaleBase saleBase { get; set; }

        [RuleRequiredField(SaleItemValidationRules.ProductIsRequired, DefaultContexts.Save)]
        [ImmediatePostData]
        IProduct Product { get; set; }

        [RuleValueComparison(SaleItemValidationRules.QuantityIsGreaterThanOrEqualZero, DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 0)]
        [ImmediatePostData]
        int Quantity { get; set; }

        [RuleValueComparison(SaleItemValidationRules.DiscountIsGreaterThanOrEqualZero, DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 0)]
        [ImmediatePostData]
        decimal Discount { get; set; }

        decimal Amount { get; set; }

        void UpdateAmount();
    }


    [DomainLogic(typeof(ISaleItem))]
    public class SaleItemLogic
    {
        public virtual decimal GetProductPrice(ISaleItem saleItem)
        {
            // supporting method to implement price lists. Dan.
            return saleItem.Product.Price;
        }
        public virtual void UpdateAmount(ISaleItem saleItem)
        {
            if (saleItem.Product != null)
            {
                saleItem.Amount = (GetProductPrice(saleItem) * saleItem.Quantity) - saleItem.Discount;
            }
            else
            {
                saleItem.Amount = 0;
            }
            if (saleItem.saleBase != null)
            {
                saleItem.saleBase.UpdateAmount();
            }
        }
        public static void AfterChange_Product(ISaleItem saleItem)
        {
            saleItem.UpdateAmount();
        }

        public static void AfterChange_Quantity(ISaleItem saleItem)
        {
            saleItem.UpdateAmount();
        }

        public static void AfterChange_Discount(ISaleItem saleItem)
        {
            saleItem.UpdateAmount();
        }
    }


    //[DefaultClassOptions]
    ////[ImageName("BO_Contact")]
    ////[DefaultProperty("DisplayMemberNameForLookupEditorsOfThisType")]
    ////[DefaultListViewOptions(MasterDetailMode.ListViewOnly, false, NewItemRowPosition.None)]
    ////[Persistent("DatabaseTableName")]
    //// Specify more UI options using a declarative approach (http://documentation.devexpress.com/#Xaf/CustomDocument2701).
    //public class ISaleItem : BaseObject
    //{ // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (http://documentation.devexpress.com/#Xaf/CustomDocument3146).
    //    public ISaleItem(Session session)
    //        : base(session)
    //    {
    //    }
    //    public override void AfterConstruction()
    //    {
    //        base.AfterConstruction();
    //        // Place your initialization code here (http://documentation.devexpress.com/#Xaf/CustomDocument2834).
    //    }
    //    //private string _PersistentProperty;
    //    //[XafDisplayName("My display name"), ToolTip("My hint message")]
    //    //[ModelDefault("EditMask", "(000)-00"), Index(0), VisibleInListView(false)]
    //    //[Persistent("DatabaseColumnName"), RuleRequiredField(DefaultContexts.Save)]
    //    //public string PersistentProperty {
    //    //    get { return _PersistentProperty; }
    //    //    set { SetPropertyValue("PersistentProperty", ref _PersistentProperty, value); }
    //    //}

    //    //[Action(Caption = "My UI Action", ConfirmationMessage = "Are you sure?", ImageName = "Attention", AutoCommit = true)]
    //    //public void ActionMethod() {
    //    //    // Trigger a custom business logic for the current record in the UI (http://documentation.devexpress.com/#Xaf/CustomDocument2619).
    //    //    this.PersistentProperty = "Paid";
    //    //}
    //}
}
